<script setup>
import { loginn,getimage } from '@/api/user.js'
import { Lock, User } from '@element-plus/icons-vue'
import { onMounted, onUnmounted, ref } from 'vue'
// import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
const form = ref()
const userStore = useUserStore()
console.log(userStore)
const router = useRouter()
// onMounted(async() => {
//   const a = await getimage()
//   console.log(a);
  
// })
onUnmounted(() => {
  document.removeEventListener('keydown', keydown, false)
})
onMounted(() => {
  // const handleKeyDown = (e) => {
  //   console.log(e.key);
  //   if(e.key == 'Enter'){
  //     login()
  //   }
  //   }
  document.addEventListener('keydown', keydown)
})
const login = async () => {
  const res = await loginn(formModel.value)
  console.log(res);
  if(res.data){
    localStorage.setItem('token',res.data)
    router.push('/layout/information')
  } else {
    alert("密码或用户名错误,请重新输入")
  }
  
}

// 对象使用的规则
const rules = {
  password: [{ required: true, message: '请输入密码', trigger: 'change' }]
}

// 整个的用于提交的form数据对象
const formModel = ref({
  user: '',
  password: ''
  // phone:'123456',
  // password:'123456'
})
const keydown = (e) => {
  if(e.key == 'Enter'){
    login()
  }
}
</script>
<template>
  <div class="page-left">
    <div>
      <img class="logo" src="../../assets/image/logogo.png" alt="" />
      <span class="biaoti">某学校校友会</span>
      <el-form
        :model="formModel"
        :rules="rules"
        ref="form"
        size="large"
        autocompleta="off"
        style="margin-top: 17px"
      >
        <el-form-item prop="username">
          <el-input
            v-model="formModel.user"
            style="width: 350px"
            :prefix-icon="User"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            style="width: 350px"
            v-model="formModel.password"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
      </el-form>
      <el-form-item>
        <el-button
          style="width: 340px; height: 40px; margin-left: 5px; font-weight: 600"
          type="primary"
          @click="login"
          @keyup.enter="keydown"
          >登录</el-button
        >
      </el-form-item>
    </div>
  </div>
  <router-view></router-view>
</template>
<style lang="scss" scoped>
.page-left {
  width: 400px;
  height: 300px;
  margin-top: 260px;
  margin-left: 560px;
  // background-color: #dd0d0d;
}
.logo {
  width: 70px;
  height: 70px;
}
.biaoti {
  float: right;
  margin-top: 15px;
  margin-right: 50px;
  padding-left: 0;
  font-size: 30px;
  font-family: 华文楷体;
  font-weight: 760;
  color: #1a3185;
}
</style>
